//
//  CorgiComposeTextView.swift
//  weibo
//
//  Created by Corgi on 17/3/28.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

class CorgiComposeTextView: UITextView {

    override var font: UIFont?{
    
        didSet {
        
            placeholderLabel.font = font
        }
    }
    
    var placeholder: String?{
    
        didSet{
        
            placeholderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    fileprivate func setupUI() {
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp.makeConstraints { (make) in
            
            make.left.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(screenWidth - 10)
        }
        
        //通知
        NotificationCenter.default.addObserver(self, selector: #selector(TextViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
        
    }
    
    fileprivate lazy var placeholderLabel: UILabel = {
    
        let lab = UILabel()
    
        lab.textColor = UIColor.darkGray
        
        lab.font = UIFont.systemFont(ofSize: 14)
    
        lab.numberOfLines = 0
        
        return lab
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

extension CorgiComposeTextView {

    @objc fileprivate func TextViewTextDidChange() {
        
        placeholderLabel.isHidden = hasText
    }
}
